package com.manbas.zhg.manbas.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.manbas.zhg.R;
import com.manbas.zhg.manbas.api.LoginApi;
import com.manbas.zhg.manbas.global.BaseActivity;
import com.manbas.zhg.manbas.utils.HttpUtis;
import com.manbas.zhg.manbas.utils.LogUtil;
import com.manbas.zhg.manbas.utils.ToastUtils;
import com.manbas.zhg.manbas.utils.UserInfoUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/11/8.
 * 登录
 */

public class LoginActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_right)
    TextView tv_right;
    @BindView(R.id.tv_left)
    TextView tv_left;
    @BindView(R.id.edt_password)
    EditText edt_psd;
    @BindView(R.id.edt_phoneNum)
    EditText edt_phoneNum;
    @BindView(R.id.tv_login)
    TextView tv_login;

    private LoginApi loginApi;

    private String phoneNum;
    private String password;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetLayout(R.layout.login_activity);
        ButterKnife.bind(this);
        initView();
    }

    private void initView(){
        tv_right.setText(R.string.register);
        tv_title.setText(R.string.login);

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void getMsg(){
        password=edt_psd.getText().toString();
        phoneNum=edt_phoneNum.getText().toString();
    }

    private void setLogin(){
        loginApi=new LoginApi();
        loginApi.password=password;
        loginApi.phone=phoneNum;
        String result= HttpUtis.api(loginApi.getUrl(),loginApi.getParamString());
        try {
            JSONObject jsonObject=new JSONObject(result);
            JSONObject jsonObject1=new JSONObject(jsonObject.optString("data"));
            String uid=jsonObject1.optString("uid");
            String token=jsonObject1.optString("token");
            UserInfoUtil.Uid=uid;
            UserInfoUtil.token=token;
            LogUtil.Log(uid+"/"+token);
            if(jsonObject.optString("code")!=null && jsonObject.optString("code").equals("200")){
                ToastUtils.showToast(LoginActivity.this,"登录成功!");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    @OnClick({R.id.tv_left,R.id.tv_right,R.id.tv_login})
    public void onClick(View v) {
        super.onClick(v);
        switch(v.getId()){
            case R.id.tv_right:
                break;
            case R.id.tv_left:
                break;
            case R.id.tv_login:
                getMsg();
//                if(!TextUtils.isEmpty(password)&&!TextUtils.isEmpty(phoneNum)){
                    setLogin();
//                }else{
//                    ToastUtils.showToast(LoginActivity.this,"填写完整信息");
//                }
                break;
        }
    }
}
